package com.woniu.adopt.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.adopt.entity.User;
import com.woniu.adopt.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class UserService {
    @Autowired
    UserMapper userMapper;

    public PageInfo<User> queryByPage(Integer now, Integer size, String name) {
        PageHelper.startPage(now, size);
        List<User> users = userMapper.selectAll(name);
        return new PageInfo<>(users);
    }

    public int add(User user) {
        return userMapper.insert(user);
    }

    public int modify(User user) {
        return userMapper.updateByPrimaryKey(user);
    }

    public int removeByKey(int id) {
        //逻辑删除
        User user = new User();
        user.setId(id);
        user.setDel(1);
        user.setUpdateTime(new Date());
        return userMapper.updateByPrimaryKey(user);

    }

    public int changeLocked(int id) {
        //切换locked的值
        User user = userMapper.selectByPrimaryKey(id);
        Integer locked = user.getLocked();
        user.setLocked(locked == 0 ? 1 : 0);
        user.setUpdateTime(new Date());
        return userMapper.updateByPrimaryKey(user);
    }

    public int judgedSameName(String userName) {
        return userMapper.judgedSameName(userName);
    }
}
